package com.samoiloff.ui.scroll.simple {
	import com.samoiloff.framework.controller.AbstractController;
	import com.samoiloff.ui.btns.frame.FrameBtnController;

	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author Samoiloff
	 */
	public class SimpleScrollController extends AbstractController {
		
		private var _model: SimpleScrollModel;

		private var _mc			: MovieClip;
		
		private var _railMc		: MovieClip;
		private var _progressMc	: MovieClip;
		
		private var _min		: Number;
		private var _max		: Number;
		private var _delta		: Number;
		
		private var _root		: DisplayObject;
		
		private var _sliderBtn	: FrameBtnController;

		
		public function SimpleScrollController(model: SimpleScrollModel) {
			super(model);
			
			_mc = model.mainMc as MovieClip;
			_railMc = _mc.railMc;
			_progressMc = _mc.progressMc;
			
			_sliderBtn	= new FrameBtnController(_mc.sliderMc);
			_sliderBtn.mc.mouseChildren = false;
			//_sliderBtn.mc.useHandCursor = false;
			
			_railMc.useHandCursor = _railMc.buttonMode = true;
			
			_min = _railMc.x;
			_max = _railMc.x + _railMc.width;
			_delta = _railMc.width;
			
			_model.addEventListener(SimpleScrollModel.PROGRESS_CHANGED, onProgressChanged);
			
			_railMc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_sliderBtn.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			onProgressChanged();
		}
		
		private function onMouseDown(e : MouseEvent) : void {
			if (!_root) {
				_root = _mc.root;
			}
			_root.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			_root.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			_mc.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			onEnterFrame();
		}
		
		private function onMouseMove(event:MouseEvent):void {
			event.updateAfterEvent();
		}

		private function onMouseUp(e : MouseEvent) : void {
			_mc.root.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			_mc.root.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			_mc.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
				
		private function onEnterFrame(e : Event = null) : void {
			getModel().progress = getProgress();
		}
		
		public function getProgress():Number {
			var y : Number = _mc.mouseX;
			
			y = y > _max ? _max : y;
			y = y < _min ? _min : y;
			
			return (y - _min) / _delta; 
		}
		
		override public function destroy(): void {
			if (_root) {
				_root.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				_root.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			}
			
			_mc.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			_railMc.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_sliderBtn.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			_model.removeEventListener(SimpleScrollModel.PROGRESS_CHANGED, onProgressChanged);
			
			_mc			= null;
		
			_railMc		= null;
			_progressMc	= null;
			
			_root		= null;
			
			_sliderBtn	= null;
			
			_model = null;
			super.destroy();
		}

		private function onProgressChanged(e:Event = null): void {
			_sliderBtn.mc.x = _min + getModel().progress * _delta;
			_progressMc.width = _railMc.width * getModel().progress;
		}
		
		private function getModel():SimpleScrollModel {
			return _model as SimpleScrollModel;
		}
	}
}
